package org.lee.export.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author jackielee
 * @version 1.0
 * @description FileUtils
 * @date 2021/5/6
 **/
public class FileUtils {
    private static final String[] PPTEXTS = {"ppt", "pptx"};
    private static final String[] IMGEXTS = {"jpg", "jpeg", "png", "bmp"};

    public static String getExt(File file) {
        String fileName = file.getName();
        int index = fileName.lastIndexOf(".");
        return index == -1 ? "" : fileName.substring(index + 1);
    }

    public static String getFileName(File file) {
        String fileName = file.getName();
        int index = fileName.lastIndexOf(".");
        return index == -1 ? fileName : fileName.substring(0, index);
    }

    public static boolean checkPpt(File file) {
        String ext = getExt(file);
        return Arrays.stream(PPTEXTS).anyMatch(x -> x.equalsIgnoreCase(ext));
    }

    public static List<File> getFiles(String path) {
        File file = new File(path);
        List<File> files = new ArrayList<>();
        if (!file.exists()) {
            return files;
        }
        if (file.isDirectory()) {
            List<File> list = Arrays.asList(file.listFiles());
            files.addAll(list);
        } else {
            files.add(file);
        }
        return files;
    }

    public static boolean checkImage(File file) {
        String ext = getExt(file);
        return Arrays.stream(IMGEXTS).anyMatch(x -> x.equalsIgnoreCase(ext));
    }
}
